package v3.object.command.list;

import galaxy.obj.Point;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Map;

import text.TextUtil;
import v3.object.STcfg;
import v3.object.Screen;
import v3.object.command.aCommand;
import v3.object.command.iCommand;
import v3.object.command.servoCommand.OnScreen;
import v3.object.ship.Ship;
import v3.object.space.Sector;
import v3.object.space.Universe;
import v3.object.space.iSpaceObj;

public class ShowSector extends aCommand
{

	int cursor = 0;

	public ShowSector()
	{
		super();
		commandName = "SHOW SECTOR";
	}

	@Override
	public void getHelp(Ship ship)
	{
		String help = commandName + " [shs]\n"
				+ "Format: " + commandName + " Show current sector\n" 
				+ "Format: " + commandName + " C Show sector according with te Cursor coodinate\n"
				+ "Display the position of the Objects ([S]tar [B]ase [E]nemy) in the sector" + "Time Cost " + ship.getTimeConsume((this.getClass())) + "\n" + "Energy Cost "
				+ ship.getEnergyConsume((this.getClass())) + "";
		help = TextUtil.formatHelp(help);
		System.out.println(help);

	}

	@Override
	protected boolean isCommandAccetable(STcfg cfg, Universe universe, Ship ship, Map<String, iCommand> commands, String[] input)
	{
		return true;
	}

	@Override
	protected double calculateEnergy(STcfg cfg, Universe universe, Ship ship, Map<String, iCommand> commands, String[] input)
	{
		return ship.getEnergyConsume((this.getClass()));
	}

	@Override
	protected double calculateTime(STcfg cfg, Universe universe, Ship ship, Map<String, iCommand> commands, String[] input)
	{
		return ship.getTimeConsume((this.getClass()));
	}

	@Override
	protected void checkInput(String[] input) throws Exception
	{
		if (input.length > 1)
			cursor = Integer.parseInt(input[1]);
	}

	@Override
	protected boolean realExecute(STcfg cfg, Universe universe, Ship ship, Map<String, iCommand> commands, String[] input)
	{
		Screen s =  new Screen(11, 44);
		s.fill(" "+String.valueOf(Character.toChars(183))+"  ");
		//System.out.println(s);
//		s.fill(".");
//		System.out.println(s);
		for(int xx=0;xx<10;xx++)
		{
			int x=xx+1;
			int y=(xx+1)*4;
			s.printAt(x,0," "+xx+" |");
			s.printAt(0,y," "+xx+" |");
		}
		Point sector = STcfg.getNewPosition(ship.getPosition(), cursor, 1);
		Sector sec =  universe.getSector(sector);
		String[][] map = sec.getSectorString();
		for (int x = 0; x < 10; x++)
		{
			for (int y = 0; y < 10; y++)
			{
				int xx=(x+1)*4;
				int yy=(y+1);
				String obj = map[x][y];
				if(obj!=null)
					s.printAt(yy, xx,  " "+obj+"  ");
				
			}
		}
		
		iCommand on = new OnScreen(commandName, s.toString());
		on.execute(cfg, universe, ship, commands, input);
		return true;
	}

	@Override
	protected boolean confirmCommand(STcfg cfg, Universe universe, Ship ship, Map<String, iCommand> commands, String[] input)
	{
		return true;
	}

}
